<?php

Yii::import('application.modules.profile.model.*');
/**
 * This is the model class for table "xman_user".
 *
 * The followings are the available columns in table 'xman_user':
 * @property integer $user_id
 * @property string $user_name
 * @property string $user_email
 * @property string $user_login
 * @property string $user_password
 * @property integer $user_is_active
 * @property string $user_avatar
 * @property string $user_mobile_phone
 * @property string $user_status
 * @property string $user_created_at_date
 * @property string $user_updated_at_date
 * @property string $user_last_login_date
 * @property integer $user_is_admin
 *
 * The followings are the available model relations:
 * @property Billing[] $billings
 * @property Bug[] $bugs
 * @property Comment[] $comments
 * @property EventUser[] $eventUsers
 * @property EventUser[] $eventUsers1
 * @property MeetingUser[] $meetingUsers
 * @property MeetingUser[] $meetingUsers1
 * @property Message[] $messages
 * @property MessageUser[] $messageUsers
 * @property MilestoneUser[] $milestoneUsers
 * @property Notebook[] $notebooks
 * @property NotebookUser[] $notebookUsers
 * @property OutsideProject[] $outsideProjects
 * @property Project[] $projects
 * @property Project[] $projects1
 * @property ProjectUser[] $projectUsers
 * @property Risk[] $risks
 * @property Schedule[] $schedules
 * @property Task[] $tasks
 * @property TaskUser[] $taskUsers
 * @property UserAcademy[] $userAcademys
 * @property UserProfile $userProfile
 * @property UserRate[] $userRates
 * @property UserSkill[] $userSkills
 * @property UserStatus $userStatus
 */
class User extends CActiveRecord
{
	public $user_password_repeat;
	public $image;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'xman_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_name, user_email, user_login, user_password, user_is_admin', 'required'),
			array('user_is_active, user_is_admin', 'numerical', 'integerOnly'=>true),
			array('user_name, user_email, user_login, user_password, user_avatar, user_status', 'length', 'max'=>50),
			array('user_mobile_phone', 'length', 'max'=>15),
			array('user_created_at_date, user_updated_at_date, user_last_login_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, user_name, user_email, user_login, user_password, user_is_active, user_avatar, user_mobile_phone, user_status, user_created_at_date, user_updated_at_date, user_last_login_date, user_is_admin', 'safe', 'on'=>'search'),
			array('user_password','compare'),
			array('user_password_repeat','safe'),
			array('image','file','types'=>'jpg,png'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'billings' => array(self::HAS_MANY, 'Billing', 'billing_created_by_user_id'),
			'bugs' => array(self::HAS_MANY, 'Bug', 'bug_posted_by_user_id'),
			'comments' => array(self::HAS_MANY, 'Comment', 'comment_posted_by_user_id'),
			'eventUsers' => array(self::HAS_MANY, 'EventUser', 'event_user_attending_user_id'),
			'eventUsers1' => array(self::HAS_MANY, 'EventUser', 'event_user_notify_to_user_id'),
			'meetingUsers' => array(self::HAS_MANY, 'MeetingUser', 'meeting_user_attending_user_id'),
			'meetingUsers1' => array(self::HAS_MANY, 'MeetingUser', 'meeting_user_notify_to_user_id'),
			'messages' => array(self::HAS_MANY, 'Message', 'message_sent_by_user_id'),
			'messageUsers' => array(self::HAS_MANY, 'MessageUser', 'message_user_to_user_id'),
			'milestoneUsers' => array(self::HAS_MANY, 'MilestoneUser', 'milestone_user_responsible_user_id'),
			'notebooks' => array(self::HAS_MANY, 'Notebook', 'notebook_posted_by_user_id'),
			'notebookUsers' => array(self::HAS_MANY, 'NotebookUser', 'notebook_user_notify_to_user_id'),
			'outsideProjects' => array(self::HAS_MANY, 'OutsideProject', 'user_id'),
			'projects' => array(self::HAS_MANY, 'Project', 'project_created_by_user_id'),
			'projects1' => array(self::HAS_MANY, 'Project', 'project_manager_id'),
			'projectUsers' => array(self::HAS_MANY, 'ProjectUser', 'project_user_user_id'),
			'risks' => array(self::HAS_MANY, 'Risk', 'risk_posted_by_user_id'),
			'schedules' => array(self::HAS_MANY, 'Schedule', 'schedule_created_by_user_id'),
			'tasks' => array(self::HAS_MANY, 'Task', 'task_created_by_user_id'),
			'taskUsers' => array(self::HAS_MANY, 'TaskUser', 'task_user_user_id'),
			'userProfile' => array(self::HAS_ONE, 'UserProfile', 'user_profile_user_id'),
			'userAcademys' => array(self::HAS_MANY, 'UserAcademy', 'user_academy_user_id'),	
			'userRates' => array(self::HAS_MANY, 'UserRate', 'user_rate_user_id'),
			'userSkills' => array(self::HAS_MANY, 'UserSkill', 'user_skill_user_id'),
			'userStatus' => array(self::HAS_ONE, 'UserStatus', 'user_status_user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'user_name' => 'User Name',
			'user_email' => 'User Email',
			'user_login' => 'User Login',
			'user_password' => 'User Password',
			'user_is_active' => 'User Is Active',
			'user_avatar' => 'User Avatar',
			'user_mobile_phone' => 'User Mobile Phone',
			'user_status' => 'User Status',
			'user_created_at_date' => 'User Created At Date',
			'user_updated_at_date' => 'User Updated At Date',
			'user_last_login_date' => 'User Last Login Date',
			'user_is_admin' => 'User Is Admin',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('user_email',$this->user_email,true);
		$criteria->compare('user_login',$this->user_login,true);
		$criteria->compare('user_password',$this->user_password,true);
		$criteria->compare('user_is_active',$this->user_is_active);
		$criteria->compare('user_avatar',$this->user_avatar,true);
		$criteria->compare('user_mobile_phone',$this->user_mobile_phone,true);
		$criteria->compare('user_status',$this->user_status,true);
		$criteria->compare('user_created_at_date',$this->user_created_at_date,true);
		$criteria->compare('user_updated_at_date',$this->user_updated_at_date,true);
		$criteria->compare('user_last_login_date',$this->user_last_login_date,true);
		$criteria->compare('user_is_admin',$this->user_is_admin);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	protected function afterValidate()
		{
		parent::afterValidate();
		$this->user_password = $this->encrypt($this->user_password);
	}
	
	public function encrypt($value){
   		return md5($value);
 	}
	
	 /**
 	 * Get list of message people asscocative with project
     */
 	public static function listUser($pid) { 
 		$projectUsers = ProjectUser::model()->findAllByAttributes(array("project_user_project_id"=>$pid));
        	
		$list = array();
        foreach ($projectUsers as $projectUser) {
        	$list[$projectUser->project_user_user_id] = $projectUser->projectUserUser->user_name;
        }
        return $list;
 	}
 	
 	/**
 	 * @return user skill
 	 */
 	public function getSkill() {
 		$userSkills = $this->userSkills;
 		$result = "";
 		$listSkill = Skill::listSkillLevel();
 		foreach ($userSkills as $userSkill) {
 			$skill = $userSkill->userSkillSkill;
 			$result .= $skill->skill_name . " - " . $listSkill[$userSkill->skill_level];
 		}
 		return $result;
 	}
 	
 	/**
 	 * @return user lastest project
 	 */
 	public function getLastestProject() {
 		
 		/* SQL command
	 		SELECT 
				p.project_name
			FROM 
				(xman_project_user as pu LEFT JOIN xman_project as p on pu.project_user_project_id = p.project_id )
			WHERE
				pu.project_user_user_id = 2
			ORDER BY p.project_end_date DESC
			LIMIT 1
 		 */
 		//return $this->user_id;
 		$criteria = new CDbCriteria();
 		$criteria->select = 'project_name';
 		$criteria->join   = 'LEFT JOIN xman_project on project_user_project_id = project_id';
 		$criteria->condition = "project_user_user_id = {$this->user_id}";
 		$criteria->order = 'project_end_date DESC';
 		$criteria->limit = '1';
 		$result = ProjectUser::model()->find($criteria);
 		return is_null($result)?NULL:$result->project_name;
 	}
 	
}